const app = getApp();
import {
  getCategoryList,
  getComment
} from '../../api/store.js';
import {
  getIndexData,
  getCategoryDetails,
  getGuide
} from '../../api/api.js';
import {
  postOrdersCreate
} from '../../api/user'
import Util from '../../utils/util.js';
import wxh from '../../utils/wxh.js';
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key: 'PTDBZ-URQWU-N3UVI-4I3UK-UZTVQ-QMFS6'
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navH: '',
    site_name: '',
    city: '', //当前选择的城市

    fl_list: [], //分类列表 
    type_index: 4, //一级选择的下标
    two_id: 0, //二级选择的下标
    info: '', //详情

    ks_addr: '', //始发地,
    js_addr: '', //目的地
    time: '', //搬家时间
    phone: '',
    mark: '',

    service_hotline: '',

    new: [],
    show_index: null,

    kf_show: false, //客服弹出
    homePageImage: '',

    type_div: '',

    tabst: 2,
    lng: '',
    lat: '',
    showModal: false,
    height: '', //屏幕高度
    markers: [],
    mark1: "../../images/maker.png",
    commentList: [],
    cityList: [],
    scale: 16,
    carst: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight
    });
    this.getType()
    this.comment()
    let that = this;
    wx.getPrivacySetting({
      success(res) {
        if (res.needAuthorization == false) {
          that.getdw()
        }
      }
    })
  },

  getlocation() {
    const that = this
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        const arr = []
        const obj = {
          id: 0,
          latitude: res.latitude,
          longitude: res.longitude,
        }
        arr.push(obj)
        that.setData({
          lat: res.latitude,
          lng: res.longitude,
          scale: 16
        })
      }
    });
  },
  // 獲取評論
  comment() {
    getComment().then(res => {
      let that = this;
      that.setData({
        commentList: res.data
      })
    }).catch(e => {})
  },
  onpreviewImage(e) {
    var src = e.currentTarget.dataset.src;
    console.log(src)
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: [src] // 需要预览的图片http链接列表
    })
  },
  getdw() {
    let that = this;
    wxh.selfLocation(1).then(res => {
      qqmapsdk.reverseGeocoder({
        location: res,
        success: function (res) {
          const arr = []
          const obj = {
            id: 0,
            latitude: res.result.ad_info.location.lat,
            longitude: res.result.ad_info.location.lng,
            callout: {
              // 点击marker展示title
              content: res.result.formatted_addresses.recommend
            }
          }
          arr.push(obj)
          that.setData({
            city: res.result.address_component.city || '',
            lat: res.result.ad_info.location.lat,
            lng: res.result.ad_info.location.lng,
            // markers: arr
          })
          wx.setStorageSync('city', res.result.address_component.city)
        },
        fail(res) {
          console.log(res, '报错了')
        },
      })
    }).catch(e => {
      console.log(e)
    })
  },


  //联系客服
  ddh() {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.service_hotline
    })
  },
  //立即咨询
  zixun() {
    wx.openCustomerServiceChat({
      extInfo: {
        url: 'https://work.weixin.qq.com/kfid/kfc63aa49c5d11725aa'
      },
      corpId: 'ww6039f159e95c0d8f',
      success(res) {}
    })
  },

  //打开客服弹窗
  open_kf() {
    this.setData({
      kf_show: !this.data.kf_show
    })
  },

  gocity() {
    wx.navigateTo({
      url: '/pages/city/index',
    })
  },

  //分类
  getType() {
    getCategoryList().then(res => {
      let that = this;
      that.setData({
        fl_list: res.data
      }, function () {
        that.setData({
          type_div: 'kuai_' + that.data.type_index
        })
        that.getxq()
      })
    }).catch(e => {})
  },
  //获取详情
  getxq() {
    let id = ''
    let item = this.data.fl_list[this.data.type_index].children
    if (!item || item.length <= 0) {
      id = this.data.fl_list[this.data.type_index].id
    } else {
      id = item[this.data.two_id].id;
    }
    getCategoryDetails({
      id: id
    }).then(res => {
      wx.hideLoading()
      this.setData({
        info: res.data
      })
    }).catch(e => {
      wx.hideLoading()
      getApp().Tips({
        title: e
      })
    })
  },

  //切换一级
  qh(e) {
    let index = e.currentTarget.dataset.index;
    if (this.data.type_index == index) {
      return
    }
    let that = this;
    wx.showLoading({
      title: ' '
    })
    that.setData({
      type_index: index,
      two_id: 0
    }, function () {
      that.getxq()
    })
  },
  //切换2级
  qh2(e) {
    let index = e.currentTarget.dataset.index;
    if (this.data.two_id == index) {
      return
    }
    let that = this;
    wx.showLoading({
      title: ' '
    })
    that.setData({
      two_id: index,
    }, function () {
      that.getxq()
    })
  },

  //选择时间
  xz_time(e) {
    this.setData({
      time: e.detail.value
    })
  },

  //打开地图选择地址
  xz_addr(e) {
    let id = '';
    let item = this.data.fl_list[this.data.type_index].children
    if (!item || item.length <= 0) {
      id = this.data.fl_list[this.data.type_index].id
    } else {
      id = item[this.data.two_id].id;
    }
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/xz_addr/index?type=' + type + '&id=' + id
    })
  },

  //打开地址薄
  go_dz(e) {
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/user_address_list/index?type=' + type,
    })
  },

  //获取input
  getinput(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      [type]: e.detail.value
    })
  },

  //预约
  yuyue() {
    if (!getApp().globalData.isLog) {
      return getApp().Tips({
        title: '请登录'
      })
    }
    let id = '';
    let item = this.data.fl_list[this.data.type_index].children
    if (!item || item.length <= 0) {
      id = this.data.fl_list[this.data.type_index].id
    } else {
      id = item[this.data.two_id].id;
    }
    console.log(this.data.ks_addr)
    let origin = this.data.ks_addr.id ? {
      id: this.data.ks_addr.id
    } : this.data.ks_addr;
    let destination = this.data.js_addr.id ? {
      id: this.data.js_addr.id
    } : this.data.js_addr;;
    let data = {
      cate_id: id,
      origin: origin,
      destination: destination,
      mark: this.data.mark,
      phone: this.data.phone,
      time: this.data.time
    }
    console.log(data)
    if (data.origin == '') {
      return getApp().Tips({
        title: '请选择始发地'
      })
    }
    if (data.destination == '') {
      return getApp().Tips({
        title: '请选择目的地'
      })
    }
    //  if(data.time == ''){
    //      return getApp().Tips({title:'请选择搬家时间'})
    //  }
    if (data.phone == '') {
      return getApp().Tips({
        title: '请输入联系电话'
      })
    }
    wx.showLoading({
      title: ' ',
    })
    postOrdersCreate(data).then(res => {
      wx.hideLoading()
      getApp().Tips({
        title: res.msg
      })
      this.setData({
        ks_addr: '',
        js_addr: '',
        phone: '',
        mark: '',
        time: ''
      })
      getApp().globalData.ks_addr = ''
      getApp().globalData.js_addr = ''
      this.setData({
        showModal: true
      })
      // wx.navigateTo({
      //   url: '/pages/bj_order/details?id=' + res.data.result.id,
      // })
    }).catch(e => {
      wx.hideLoading()
      getApp().Tips({
        title: e.msg || e
      })
    })
  },

  getcar() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      carst: false
    })
    getIndexData({
      city_name: wx.getStorageSync('city')
    }).then(res => {
      this.setData({
        site_name: res.data.site_name,
        service_hotline: res.data.service_hotline,
        homePageImage: res.data.homePageImage,
        cityList: res.data.info.cars,
        cityprice: res.data.info.cars.length > 0 ? Math.floor(res.data.info.cars[0].price) : "0"
      })
      if (res.data.info.cars) {
        let arr = []
        console.log("index", res.data.info.cars)
        res.data.info.cars.forEach((item, index) => {
          const obj = {
            id: index,
            latitude: item.lat,
            longitude: item.long,
            callout: {
              // 点击marker展示title
              content: item.city
            }
          }
          arr.push(obj)
        });
        this.setData({
          markers: arr,
          scale: 10,
          carst: true
        })
        wx.hideLoading()
      }
    }).catch(e => {})
  },

  getindex() {
    getIndexData({
      city_name: wx.getStorageSync('city')
    }).then(res => {
      this.setData({
        site_name: res.data.site_name,
        service_hotline: res.data.service_hotline,
        homePageImage: res.data.homePageImage,
        cityList: res.data.info.cars,
        cityprice: res.data.info.cars.length > 0 ? Math.floor(res.data.info.cars[0].price) : "0",
        scale: 16
      })
      if (res.data.info.cars) {
        let arr = []
        console.log("index", res.data.info.cars)
        res.data.info.cars.forEach((item, index) => {
          const obj = {
            id: index,
            latitude: item.lat,
            longitude: item.long,
            callout: {
              // 点击marker展示title
              content: item.city
            }
          }
          arr.push(obj)
        });
        this.setData({
          markers: arr,
        })
      }
    }).catch(e => {})
  },

  //手风琴 
  toogle(e) {
    let index = e.currentTarget.dataset.index;
    if (index == this.data.show_index) {
      this.setData({
        show_index: null
      })
    } else {
      this.setData({
        show_index: index
      })
    }
  },

  gopage(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },

  // 点击用户评价
  swichleft() {
    this.setData({
      tabst: 2
    })
  },

  swichuright() {
    this.setData({
      tabst: 1
    })
  },
  onpop() {
    this.setData({
      showModal: true
    })
  },
  onclose() {
    this.setData({
      showModal: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    // 动态获取屏幕高度
    wx.getSystemInfo({
      success: (result) => {
        that.setData({
          height: result.windowHeight
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getindex()
    this.setData({
      city: wx.getStorageSync('city') || '',
      ks_addr: getApp().globalData.ks_addr || '',
      js_addr: getApp().globalData.js_addr || '',
    })
    getGuide({
      type: 1
    }).then(res => {
      this.setData({
        new: res.data
      })
    }).catch(e => {})
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})